﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Avatar.Model;
using AvatarHelper;
using Avatar.BLL;

namespace Avatar.MembershipService
{
    public static class RoleManager
    {
        /// <summary>
        /// 添加访问权限
        /// </summary>
        /// <param name="_uid"></param>
        /// <param name="_menuID"></param>
        /// <returns></returns>
        public static void UpdateRole(string _groupName, int _menuID)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _role = _adc.AdministratorAccessRole.Where(r => r.GroupName == _groupName && r.MenuID == _menuID).FirstOrDefault();
                if (_role == null)
                {
                    _role = new AdministratorAccessRole();
                    _role.GroupName = _groupName;
                    _role.MenuID = _menuID;

                    _adc.AdministratorAccessRole.InsertOnSubmit(_role);
                }
                else
                {
                    _adc.AdministratorAccessRole.DeleteOnSubmit(_role);
                }
                _adc.SubmitChanges();
            }
        }

        /// <summary>
        /// 获取用户访问权限列表
        /// </summary>
        /// <param name="_uid"></param>
        /// <returns></returns>
        public static List<AdministratorAccessRole> GetUserAccessRole(string _gName)
        {
            return DBHelper.GetAll<AdministratorAccessRole>(r => r.GroupName == _gName, "获取用户访问权限列表失败:");
        }

        /// <summary>
        /// 根据菜单项取得可访问此菜单项的用户组
        /// </summary>
        /// <param name="_menuID"></param>
        /// <returns></returns>
        public static List<AdministratorAccessRole> GetUserAccessRole(int _menuID)
        {
            return DBHelper.GetAll<AdministratorAccessRole>(r => r.MenuID == _menuID, "获取用户访问规则出错");
        }
    }
}
